package com.banciyuan.bcywebview.biz.register;


import android.content.Intent;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.api.UserApi;
import com.banciyuan.bcywebview.base.helper.data.UserDataHelper;
import com.banciyuan.bcywebview.base.view.toast.MyToast;
import com.banciyuan.bcywebview.utils.http.BcyPair;
import com.banciyuan.bcywebview.utils.http.HttpUtils;
import com.banciyuan.bcywebview.utils.http.StatusCodeUtils;
import com.banciyuan.bcywebview.utils.http.StringRequestParam;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


public class OldPhoneValidActivity extends PhoneValidActivity {
    private static int GO_REBINDPHONE = 1001;

    protected void bindMobile() {
        if (is_send) return;
        is_send = true;
        String url = HttpUtils.BASE_URL + UserApi.verifyOldMobile();
        List<BcyPair> paramsList = new ArrayList<>();
        paramsList.add(new BcyPair(HttpUtils.TOKEN, UserDataHelper.getInstance(this).getToken()));
        paramsList.add(new BcyPair(HttpUtils.CODE, code));
        paramsList.add(new BcyPair(HttpUtils.MOBILE, phoneNum));
        HashMap<String, String> params = HttpUtils.getData(paramsList);
        Response.Listener succ = new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                is_send = false;
                try {
                    JSONObject result = new JSONObject(s);
                    MyToast.show(OldPhoneValidActivity.this, result.getString(HttpUtils.UPLOAD_DATA));
                    if ("1".equals(result.getString("status"))) {
                        Intent intent = new Intent(OldPhoneValidActivity.this, RebindPhoneActivity.class);
                        startActivityForResult(intent, GO_REBINDPHONE);
                    } else {
                        resetCodeHandler();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };

        Response.ErrorListener error = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                is_send = false;
            }
        };
        StringRequestParam stringRequest = new StringRequestParam(Request.Method.POST, url, params, succ, error);
        mQueue.add(stringRequest);
    }

    @Override
    protected void initUi() {
        super.initUi();
        mEtPhone.setHint(getString(R.string.input_current_binded_phonenum));
        mTvPageTitle.setText(getString(R.string.old_phone_verify));
    }

    protected void gotoNextActivity() {
        finish();
    }

    protected void requestCode() {
        if (has_post) return;
        has_post = true;
        String url = HttpUtils.BASE_URL + UserApi.getRebindMobileCode();
        List<BcyPair> paramsList = new ArrayList<>();
        paramsList.add(new BcyPair(HttpUtils.TOKEN, UserDataHelper.getInstance(this).getToken()));
        paramsList.add(new BcyPair(HttpUtils.TYPE, "rebind"));
        paramsList.add(new BcyPair(HttpUtils.MOBILE, phoneNum));
        HashMap<String, String> params = HttpUtils.getData(paramsList);
        Response.Listener succ = new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                has_post = false;
                try {
                    if (StatusCodeUtils.dealStatusCode(s, OldPhoneValidActivity.this)) {
                        JSONObject result = new JSONObject(s);
                        MyToast.show(OldPhoneValidActivity.this, result.getString(HttpUtils.UPLOAD_DATA));
                    } else {
                        resetCodeHandler();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };

        Response.ErrorListener error = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                has_post = false;
            }
        };
        StringRequestParam stringRequest = new StringRequestParam(Request.Method.POST, url, params, succ, error);
        mQueue.add(stringRequest);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == GO_REBINDPHONE) {
            setResult(resultCode);
            finish();
        }
    }
}
